#include <iostream>
#include <string>
#include "Image.H"
#include "IO.H"

using namespace pip;
using namespace std;



int main(int argc, char* argv[]) {
// initialization of variables
	int* aiHist;
//histogram array
	// aiHist = new int[256] {}; ??
	aiHist = new int[256];
	string  sIma1, sIma2, sIma3;
	char* cIma[] = {"lena.bmp","rectangle.bmp","ramp.bmp"}; //names given
	bool   bLoad;
	for(int i=0; i<256;i++)
	{
		aiHist[i]=0;
		//cout << aiHist[i];
	}
	Image<unsigned char> aImage;
//list options and get operands
	cout << "##### #### ### ## #   Loading a image dialog!   # ## ### #### #####" 
		 << endl 
	     << "Listing of available files:" << endl;
	for(int i=0;i<3;i++)
	{
		cout << cIma[i] << endl;
	}
	cout << "choose one Image for histogram: " << endl;
	cin >> sIma1; //>> sIma2;
//check if operands are from given pictures
	for(int i=0;i<3;i++)
	{
		if((strcmp(cIma[i],sIma1.c_str())==0) 
			|| (strcmp(cIma[i],sIma2.c_str())==0))
		bLoad=1;
	}
//load operands	
	importFile(aImage, sIma1);
	//importFile(bImage, sIma2);
//check if image is available
	if(bLoad)
	{
//more variables
	    int x_max = aImage.dim(0);
		int y_max = aImage.dim(1);
		int iMax=-999999;
		int iXmax=-1;
		int iMin=999999; 
		//cout << cImage.dim(0) <<" "<< cImage.dim(1) <<" "<< aImage.dim(0) <<" "<< aImage.dim(1) << endl;
//loop for all pixels
		for (int x=0; x < x_max; x++)
		{
			for (int y=0; y < y_max;y++)
			{
//count occurrences
					aiHist[aImage(x,y)]++;
				//cout << x << " " << y << endl;
				//iTmp= (bImage(x,y)-128 - aImage(x,y)-128);
//find max of the occurrences - the min might be a non global minimum
					if(aiHist[aImage(x,y)] > iMax)
					{
						iMax=aiHist[aImage(x,y)];
						iXmax=aImage(x,y);
					}
				if(aiHist[aImage(x,y)]<iMin)
					iMin=aiHist[aImage(x,y)];
				//if(iTmp>255)
				//	iTmp=255;
				//if(iTmp<0)
				//	iTmp=0;
				//cout << iTmp << endl;
				//cout << (unsigned char)iTmp << endl;
				//system("pause");
			}
		}
		cout << "    Max: " << iMax << endl 
			<< "Histogram at " << iXmax << " is " << aiHist[iXmax]
		<< endl << "type 't' to see the whole histogram table:";
		cin >> sIma2;
//check if table shoud be printed
		if(strcmp("t",sIma2.c_str())==0)
		{
//show the table
			cout << "histogram table:"<<endl;
			for(int i=0; i<256;i++)
			{
				cout << i << " " << aiHist[i] << endl ;
			}
		}
		//		if(iMin < 0) 
		//		{
		//			cout <<" min kleiner null "<<endl;
		//		if(iMax > 255)
		//				cout <<"min kleiner null und max groesser 255 "<<endl;
		//		}else if(iMax > 255)
		//		{
		//			cout <<"min groesser null max groesser 255 "<<endl;
		//		}
		//		cout << (int)bImage(256,256) << "  " << (int)bImage(1,1) << endl;
		//system("pause");
//loop again
//and find the minimal difference between two frequencies
		double iDiff_min = 999999;
		for(int j=0; j < 256; j++)
		{
			for(int k=0; k < 256; k++)
			{
				if((aiHist[j] != aiHist[k]) && (iDiff_min > abs(aiHist[j]-aiHist[k])))
					iDiff_min=abs(aiHist[j]-aiHist[k]);
			}
		}
		//cout << iMax << " " << iDiff_min << endl;
//find the hight of the histogram
//RELATIVE with all differences:
		double H = iMax/iDiff_min;
//RELATIVE with 100 percent
		//H=100;
//reserve picture for histogram
		Image<unsigned char> cImage(Dimension(256,H));
//loop through colums
		for (int x=0; x < 256; x++)
		{
//how many percentage of the full scale is shown in this column?
			double iTmp = (double)aiHist[x] / (double)iMax;
			//cout << iTmp << " " << H - (iTmp*H) << " ai "<< aiHist[x] << " max "<< iMax << " h "<< H <<endl;
//draw this percantage 
			for (int y=0; y <= H;y++)
			{
				if(y < (H - (iTmp*H)))
				{
//black above
					cImage(x,y)=0;
				}
				else
				{
//white below == occurrence
					cImage(x,y)=255;
				}

			}
		}
//save file to disk
		exportFile(cImage,"histogram_min_diff2.bmp");
//calculate addition step
				//iTmp= (bImage(x,y)-128 - aImage(x,y)-128);
//find out how values must be mapped in between 0-255
				//if(iMin < 0) 
				//{
				////	iTmp = iTmp + abs(iMin);
				//	if( (iMax+abs(iMin)) > 255)
				//		//iTmp = iTmp * 254/(iMax+abs(iMin)); //254 should be 255
				//}
				//else if(iMax > 255)
				//{
				//	//iTmp = iTmp * 254/iMax; //254 should be 255
				//}
//if pixel is invalid, i.e. non unsigned char
				//if(iTmp>255 || iTmp <0)
					//cout << iTmp << " is a FATAL VALUE " << endl;
// save result in new image
				//cImage(x,y) = (unsigned char)iTmp;
		//cout << iMax << endl;
		//cout << iMin << endl;
//write image to disk via pip-lib
		
//TEST for pixel values
//		system("pause");
//		Image<unsigned char> dImage;
//		importFile(dImage,"sumImage2.bmp");
//		for (int x=0; x < 512; ++x)
//		{
//			for (int y=0; y < 512;++y)
//			{
//				//cout << iTmp << endl;
//				//cout << (unsigned char)iTmp << endl;
//				//system("pause");
//				cout << dImage(x,y) << "  "; 
//			}
//		}
//	}
//original

/*  cout << "Welcome!" << endl;
  cout << "Don't forget to smile today :) !" << endl;

  Image<unsigned char> image;
  importFile(image, "lena.bmp"); // replace "lena.bmp" during the assignment
  
  system("pause");
 */
	// initialization of vars

// mean pixel value
/*
	// repeating the mainloop for all images
	// ask for filename	
		cout << "##### #### ### ## #   Loading a image dialog!   # ## ### #### #####" 
			 << endl 
		<< "Listing of available files:" << endl;
		for(int i=0;i<3;i++)
		{
			cout << cIma[i] << endl;
		}
				// listing files in current directory
				//system("dir /B *.bmp");
				//cout << endl << "Please enter one of the listed files:";
	// read target/filename
		cout << endl << "Please enter one of the listed files:";
		cin >> sIma;
			/*
			// construct command:'dir [FILE] /B'
			sTmp = sTmp.append(sIma);
			sTmp = sTmp.append(" ");
			sTmp = sTmp.append(sIma);
			sReturn = system(sTmp.c_str());
			cout <<endl << "NBR" << endl << sReturn << endl << atoi(sReturn.c_str()) << endl << sReturn.length() << endl;
			//if file is available  
			if(atoi(sReturn.c_str()) == 0)
			*/
/*		bLoad = 0;
		for(int i=0;i<3;i++)
		{
			if(strcmp(cIma[i],sIma.c_str())==0)
				bLoad=1;
		}
		if(bLoad==1)
		{
			// loading the file
			cout << "loading "<< sIma << "..." << endl;
			importFile(aImage, sIma);
		}
		else
		{
			cout << "this file is not here" << endl;
		}
		cout << "giving mean pixel value of " << sIma << endl;
		float sum = 0;
		int             x_max = aImage.dim(0);
		int             y_max = aImage.dim(1);
		for (int x=0; x < x_max; x++)
		{
			for (int y=0; y < y_max;y++)
			{
				sum += aImage(x,y);
				//cout << x << " " << y << endl;
			}
		}
		sum = sum/(y_max*x_max);
		cout << sIma << "'s mean pixel value is: " << sum << endl;
	}
*/
}
		system("pause");
  return 0;
}
